﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using PostRequestFactory.Core;
using PostRequestFactory.Core.Interfaces;
using PostRequestFactory.Core.Parameters;

namespace PostRequestFactory.TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Testing request based on BasicRequestParameter

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Testing request based on BasicRequestParameter.{0}", Environment.NewLine);

            var webClient = new WebClient ();

            var paramList =
                new List<IRequestParameter>
                    {
                        new BasicRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
                        new BasicRequestParameter("value2", "AbcȘaBc")
                    };

            var request = RequestFactory.CreateRequest(paramList);

            webClient.Headers.Add("Content-Type", request.ContentTypeHeader);

            var response = 
                webClient.UploadData("http://192.168.1.8/PostRequestFactoryTestServer/Home/About", request.BodyAsBytes);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(Encoding.UTF8.GetString(response) + Environment.NewLine);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Finished test.{0}",Environment.NewLine);

            #endregion

            #region Testing request based on TextRequestParameter

            Console.WriteLine("Testing request based on TextRequestParameter.{0}", Environment.NewLine);
            Console.ForegroundColor = ConsoleColor.White;

            webClient = new WebClient {};

            paramList =
                new List<IRequestParameter>
                    {
                        new TextRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
                        new TextRequestParameter("value2", "cccĂĂĂ")
                    };

            request = RequestFactory.CreateRequest(paramList);

            webClient.Headers.Add("Content-Type", request.ContentTypeHeader);

            response = webClient.UploadData("http://192.168.1.8/PostRequestFactoryTestServer/Home/About", request.BodyAsBytes);


            Console.WriteLine(Encoding.UTF8.GetString(response) + Environment.NewLine);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Finished test.{0}", Environment.NewLine);

            #endregion

            #region Testing request based on TextRequestParameter with base64

            Console.WriteLine("Testing request based on TextRequestParameter with base64.{0}", Environment.NewLine);
            Console.ForegroundColor = ConsoleColor.White;

            webClient = new WebClient {};

            paramList =
                new List<IRequestParameter>
                    {
                        new TextRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ",true, Encoding.UTF8),
                        new TextRequestParameter("value2", "cccĂĂĂ")
                    };

            request = RequestFactory.CreateRequest(paramList);

            webClient.Headers.Add("Content-Type", request.ContentTypeHeader);

            response = webClient.UploadData("http://192.168.1.8/PostRequestFactoryTestServer/Home/About", request.BodyAsBytes);

            Console.WriteLine(Encoding.UTF8.GetString(response) + Environment.NewLine);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Finished test.{0}", Environment.NewLine);

            #endregion

            #region Testing request based on BinaryRequestParameter

            Console.WriteLine("Testing request based on BinaryRequestParameter.{0}", Environment.NewLine);
            Console.ForegroundColor = ConsoleColor.White;

            webClient = new WebClient {};

            paramList =
                new List<IRequestParameter>
                    {
                        new BinaryRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
                        new BinaryRequestParameter("value2", "cccĂĂĂ"),
                        new BinaryRequestParameter("value3",File.ReadAllBytes("PostRequestFactory.TestConsole.exe"),"PostRequestFactory.TestConsole.exe")
                    };

            request = RequestFactory.CreateRequest(paramList);

            webClient.Headers.Add("Content-Type", request.ContentTypeHeader);

            response = webClient.UploadData("http://192.168.1.8/PostRequestFactoryTestServer/Home/About", request.BodyAsBytes);

            Console.WriteLine(Encoding.UTF8.GetString(response) + Environment.NewLine);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Finished test.{0}", Environment.NewLine);

            #endregion

            #region Testing request based on BinaryRequestParameter as base64

            Console.WriteLine("Testing request based on BinaryRequestParameter as base64.{0}", Environment.NewLine);
            Console.ForegroundColor = ConsoleColor.White;

            webClient = new WebClient {};

            paramList =
                new List<IRequestParameter>
                    {
                        new BinaryRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
                        new BinaryRequestParameter("value2", "cccĂĂĂ",true),
                        new BinaryRequestParameter("value3",File.ReadAllBytes("PostRequestFactory.TestConsole.exe"),"PostRequestFactory.TestConsole.exe",true)
                    };

            request = RequestFactory.CreateRequest(paramList);

            webClient.Headers.Add("Content-Type", request.ContentTypeHeader);

            response = webClient.UploadData("http://192.168.1.8/PostRequestFactoryTestServer/Home/About", request.BodyAsBytes);

            Console.WriteLine(Encoding.UTF8.GetString(response) + Environment.NewLine);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Finished test.{0}", Environment.NewLine);

            #endregion

            Console.ReadKey();
        }
    }
}
